<template>
  <div class='table-tree'>
    <ul class="table-header">
      <li v-for="(th, h) in column" :key="h">{{th.title}}</li>
    </ul>
    <ul class="table-body" v-if="data.length === 0">
      暂无数据
    </ul>
    <div class="table-body" v-else>
      <div v-for="(tr, r) in data.length" :key="r" :class="{active: r === activeTR, tr}">
        <div class="item show">
          <div class="inner" v-if="expand" @click="clickTR(data[r], r)">
            <span v-if="data[r].children"> + </span>
            <span v-else> - </span>
          </div>
          <div class="inner" v-for="(td, key) in data[r].value" :key="key">
            <span>{{td}}</span>
          </div>
        </div>
        <div v-show="data[r].show" style="width:100%; background: #555;">
          <table-tree style="background:none" v-if="data[r].children" :column="column" :data="data[r].children"></table-tree>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'tableTree',
  props: {
    column: {
      type: Array,
      required: true,
      default: function () {
        return []
      }
    },
    data: {
      type: Array,
      default: function () {
        return []
      }
    },
    showPage: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      activeTR: null
    }
  },
  computed: {
    expand () {
      let result = false
      this.column.forEach(col => {
        if (col.key === 'expand') {
          result = true
        }
      })
      return result
    }
  },
  methods: {
    clickTR (tr, index) {
      tr.show = !tr.show
      if (this.activeTR === index) {
        this.activeTR = null
      } else {
        this.activeTR = index
      }
    }

  }
}
</script>

<style scoped>
.table-tree * {
  box-sizing: border-box;
}
.table-tree {
  background: #ccc;
}

.table-tree .table {
  width: 100%;
  min-width: 200px;
  min-height: 100px;
  border-collapse:collapse;
}

.table td {
  min-height: 40px;
  min-width: 100px;
}

.table-header {
  display: flex;
}

.table-header li {
  flex: 1;
}

.table-header{
  list-style: none;
}

.table-body .tr {
  display: flex;
  flex-wrap: wrap;
}

.table-body .active {
  background: #333;
  color: #fff;
}

.table-body .item {
  width: 100%;
}

.tr .show {
  display: flex;
}

.show .inner {
  flex: 1;
}

.inner {
  border: 1px solid #000;
}

.table-body .tr {
  cursor: pointer;
}

.table-body .tr:hover {
  background: #eee;
  color: #000;
}

.table-footer .page {
  text-align: right;
}
</style>
